import sys
import re


def count_characters(text):

    return len(text)


def count_words(text):

    return len(re.findall(r'\b\w+\b', text))


def count_sentences(text):

    sentences = text.split('.')
    sentences = [sentence.strip() for sentence in sentences if sentence.strip()]
    return len(sentences)


def count_code_lines(text):

    lines = text.split('\n')
    code_lines = [line for line in lines if not line.strip().startswith('#') and line.strip()]
    return len(code_lines)


def count_empty_lines(text):

    lines = text.split('\n')
    empty_lines = [line for line in lines if not line.strip()]
    return len(empty_lines)


def count_comment_lines(text):

    lines = text.split('\n')
    comment_lines = [line for line in lines if line.strip().startswith('#')]
    return len(comment_lines)


def main(file_path, mode):
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            text = file.read().replace('\r', ' ')

            if mode == '-c':
                print(f"字符数量: {count_characters(text)}")
            elif mode == '-w':
                print(f"单词数量: {count_words(text)}")
            elif mode == '-s':
                print(f"句子数量: {count_sentences(text)}")
            elif mode == '-l':
                print(f"代码行数: {count_code_lines(text)}")
            elif mode == '-e':
                print(f"空行数量: {count_empty_lines(text)}")
            elif mode == '-m':
                print(f"注释行数: {count_comment_lines(text)}")
            else:
                print("Invalid mode. Use -c, -w, -s, -l, -e, or -m.")
    except FileNotFoundError:
        print("The file was not found. Please check the file path.")
    except Exception as e:
        print(f"An error occurred: {e}")


if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage: python wc.py [-c|-w|-s|-l|-e|-m] file.txt")
    else:
        mode = sys.argv[1]
        file_path = sys.argv[2]
        main(file_path, mode)